package com.sanhe.rfmuscle.util;

import android.util.Log;

import com.menu.app.MyApp;
import com.plata.base.aclr.utils.SPHelper;
import com.sanhe.rfmuscle.bean.ParamBean;
import com.sanhe.rfmuscle.event.Event;

public class ParamHelper {
    private static final String TAG = "ParamHelper";

    private static final String SPORT_HEAD = "sport_";
    private static final String GENTLE_HEAD = "gentle_";

    private static final String LEFT_UPPER = "left_upper";
    private static final String RIGHT_UPPER = "right_upper";
    private static final String LEFT_FOREARM = "left_forearm";
    private static final String RIGHT_FOREARM = "right_forearm";
    private static final String ABDOMEN = "abdomen";
    private static final String LEFT_THIGH = "left_thigh";
    private static final String RIGHT_THIGH = "right_thigh";
    private static final String LEFT_SHIN = "left_shin";
    private static final String RIGHT_SHIN = "right_shin";
    private static final String LEFT_UPPER_BACK = "left_upper_back";
    private static final String RIGHT_UPPER_BACK = "right_upper_back";
    private static final String LEFT_FOREARM_BACK = "left_forearm_back";
    private static final String RIGHT_FOREARM_BACK = "right_forearm_back";
    private static final String BACK = "back";
    private static final String LEFT_THIGH_BACK = "left_thigh_back";
    private static final String RIGHT_THIGH_BACK = "right_thigh_back";
    private static final String LEFT_SHIN_BACK = "left_shin_back";
    private static final String RIGHT_SHIN_BACK = "right_shin_back";

    public static boolean saveParam(int bodyPart) {
        String key = getKey(bodyPart);
        if (key == null) {
            return false;
        }
        final int rf, rf2;
        if (Const.mode == 2 && Event.rfShowEvent.getValue() != 0) {
            rf = Event.leftRfEvent.getValue();
            rf2 = Event.rightRfEvent.getValue();
        } else {
            rf = 0;
            rf2 = 0;
        }
        final int level;
        if (Const.mode == 1 && Event.sportModeEvent.getValue() > 0){
            level = Event.sportModeEvent.getValue();
        } else if (Const.mode == 2&& Event.workingModeEvent.getValue() > 0) {
            level =Event.workingModeEvent.getValue();
        } else {
            level = 1;
        }
        ParamBean paramBean = new ParamBean(Event.leftTimeEvent.getValue(), Event.leftProgressEvent.getValue()
                , Event.timeEvent.getValue(), Event.rightProgressEvent.getValue(), rf, rf2, level);
        String param = GsonManager.getInstance().toJson(paramBean);
        SPHelper.getInstance(MyApp.getApp()).putString(key, param).commit();
        return true;
    }


    public static ParamBean getParam(int bodyPart) {
        String key = getKey(bodyPart);
        if (key == null) {
            return null;
        }
        String param = SPHelper.getInstance(MyApp.getApp()).getString(key, null);
        if (param == null) {
            Log.e(TAG, "getParam: param == null");
            int index = bodyPart - 1;
            // 没有找到的数据从这里拿
            ParamBean paramBean = new ParamBean();
            if (Const.mode == 1) {
                paramBean.setTime1(Const.sportParam[index][0]);
                paramBean.setTime2(Const.sportParam[index][0]);
                paramBean.setFreq1(Const.sportParam[index][1]);
                paramBean.setFreq2(Const.sportParam[index][1]);
                paramBean.setLevel(Const.sportParam[index][2]);
                return paramBean;
            } else if (Const.mode == 2){
                paramBean.setTime1(Const.gentleParam[index][0]);
                paramBean.setTime2(Const.gentleParam[index][0]);
                paramBean.setFreq1(Const.gentleParam[index][1]);
                paramBean.setFreq2(Const.gentleParam[index][1]);
                paramBean.setLevel(Const.gentleParam[index][2]);
                paramBean.setRf1(Const.gentleParam[index][3]);
                paramBean.setRf2(Const.gentleParam[index][3]);
                return paramBean;
            }
            return null;
        }
        ParamBean paramBean = GsonManager.getInstance().fromJson(param, ParamBean.class);
        Log.e(TAG, "getParam: update success");
        Log.e(TAG, paramBean.toString());
        return paramBean;
    }

    public static boolean removeParam(int bodyPart) {
        String key = getKey(bodyPart);
        if (key == null) {
            return false;
        }
        SPHelper.getInstance(MyApp.getApp()).remove(key).commit();
        return true;
    }

    private static String getKey(int bodyPart) {
        String head, end;
        switch (Const.mode) {
            case 1 -> head = SPORT_HEAD;
            case 2 -> head = GENTLE_HEAD;
            default -> {
                return null;
            }
        }
        switch (bodyPart) {
            case 1 -> end = LEFT_UPPER;
            case 2 -> end = RIGHT_UPPER;
            case 3 -> end = LEFT_FOREARM;
            case 4 -> end = RIGHT_FOREARM;
            case 5 -> end = ABDOMEN;
            case 6 -> end = LEFT_THIGH;
            case 7 -> end = RIGHT_THIGH;
            case 8 -> end = LEFT_SHIN;
            case 9 -> end = RIGHT_SHIN;
            case 10 -> end = LEFT_UPPER_BACK;
            case 11 -> end = RIGHT_UPPER_BACK;
            case 12 -> end = LEFT_FOREARM_BACK;
            case 13 -> end = RIGHT_FOREARM_BACK;
            case 14 -> end = BACK;
            case 15 -> end = LEFT_THIGH_BACK;
            case 16 -> end = RIGHT_THIGH_BACK;
            case 17 -> end = LEFT_SHIN_BACK;
            case 18 -> end = RIGHT_SHIN_BACK;
            default -> {
                return null;
            }
        }
        return head + end;
    }
}
